// 1、无头单向非循环链表实现
public class SingleLinkedList {
    public SingleLinkedList() {

    }
    static class ListNode {
        public int numb ;
        public ListNode next ;

        public ListNode(int numb) {
            this.numb = numb;
        }
    }

    public ListNode head;
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
       if(head == null) {
           head = node;
       }else {
           ListNode tmp = this.head;
           while(tmp.next != null) {
               tmp = tmp.next;
           }
           tmp.next = node;
       }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        ListNode cur = new ListNode(data);
        if(index < 0 || index >size()) {
            throw new NodeError("位置不合法"+index);
        }else if(index == 0) {
            cur.next = head;
            head = cur;
            return true;
        } else if(index == size()) {
            ListNode temp = this.head;
            while(temp.next != null) {
                temp = temp.next;
            }
            temp.next = cur;
            return true;
        }else {
            ListNode temp = searchPer(index);
            cur.next = temp.next;
            temp.next = cur;
            return true;
        }
    }
    //找前一个节点
    private ListNode searchPer(int index) {
        ListNode cur = this.head;
        while((index - 1) != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.numb == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(this.head == null) {
            return;
        }
        ListNode cur = findPer(key);
        if(cur == null) {
            return;
        } else if(cur == this.head) {
            head = null;
        } else {
            ListNode temp = cur.next;
            cur.next = temp.next;
            temp.next = null;
        }
    }
    //找前一个节点
    private ListNode findPer(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.next.numb == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }
       ListNode cur = head.next;
       ListNode pre = head;
       while(cur != null) {
           if(cur.numb == key) {
               pre.next = cur.next;
               cur = cur.next;
           }else {
               pre = cur;
               cur = cur.next;
           }
       }
       if(head.numb == key) {
           head = head.next;

       }
    }
    //得到单链表的长度
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.numb + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear() {
        head = null;
    }
}